package com.wisdomparking.control.web;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.wisdomparking.bean.Manager;
import com.wisdomparking.config.result.TempResult;
import com.wisdomparking.constant.Constants;
import com.wisdomparking.control.BaseController;
import com.wisdomparking.service.ManagerService;
import com.wisdomparking.tools.MD5;
import com.wisdomparking.tools.ToolString;
import com.wisdomparking.tools.ToolValid;


/**
 * 订单中心
 */
@RestController
@RequestMapping("/view")
public class ViewController extends BaseController{
	
	@Autowired ManagerService managerService;
	
	/**
	 * 注册页
	 * @return
	 */
	@RequestMapping("/register")
	public ModelAndView register(){
		
		return new ModelAndView("web/register");
	}
	
	
	
	/**
	 * 验证帐号
	 * 
	 * @param mobile
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/checkAccount")
	public void checkAccount(String userName) throws IOException{
		
		boolean flag = false;
		
		Manager manager = managerService.getManagerByUserName(userName);
		
		if(ToolValid.isNull(manager)){
			flag = true;
		}
		
		response.reset();
		response.setContentType("text/html;charset=UTF-8");
		response.getOutputStream().print(flag);
		response.getOutputStream().close();
	}
	
	
	/**
	 * 验证手机号是否绑定
	 * 
	 * @param mobile
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	@RequestMapping(value = "/checkMobile")
	public void checkMobile(String mobile) throws IOException{
		boolean flag = false;
		
		Manager manager = managerService.getManagerByMobile(mobile);
		if(ToolValid.isNull(manager)){
			flag = true;
		}
		
		response.reset();
		response.setContentType("text/html;charset=UTF-8");
		response.getOutputStream().print(flag);
		response.getOutputStream().close();
	}
	
	/**
	 * 发送短信验证码
	 * 
	 * @param mobile
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/sendCheckCode")
	public TempResult sendCheckCode(String mobile) throws IOException{
		String code = ToolString.generateIdentifyingCode();
		boolean flag = true; // ToolSMS.sendMobileMsg(mobile, code);
		return new TempResult(flag, code);
	}
	
	
	/**
	 * 后台用户登录
	 * 
	 * @param userName
	 * @param password
	 */
	@RequestMapping(value = "/login")
	public TempResult login(String userName, String password){
		boolean flag = false;
		
		Manager manager = managerService.getManagerByUserName(userName);
		if(ToolValid.isNotNull(manager)){
			if(manager.getPassword().equals(MD5.getMD5Str(password))){
				session.setAttribute(Constants.LOGININFO, manager);
				flag = true;
			}
		}
		
		return new TempResult(flag, "");
	}
	
	/**
	 * 退出
	 * 
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value = "/exit")
	public void exit() throws IOException{
		session.removeAttribute(Constants.LOGININFO);
		response.sendRedirect(request.getContextPath() + "/login");
	}
}
